﻿using System;
using System.Collections;
using System.Collections.Generic;
using App.Procedure;
using Framework.Core;
using Framework.Utilities;
using UnityEngine;
using UnityEngine.UI;

namespace App.UI
{
    /// <summary>
    /// 登录界面。
    /// </summary>
    public class LoginUI : UIBase
    {
        private MessageQueue<LoginMsg> _messageQueue;

        public override void OnRegisterHandler()
        {
            base.OnRegisterHandler();
            _messageQueue = MessageManager.Instance.Get<LoginMsg>();

            _messageQueue.RegisterHandler(LoginMsg.LoginReply, LoginResult);
        }

        /// <summary>
        /// 当按下帮助按钮时。
        /// </summary>
        public void OnHelpClick()
        {
            UIManager.Instance.Open("DialogUI", 2, Localization.Instance.GetText("LoginHelp"));
        }

        /// <summary>
        /// 当按下登录按钮时。
        /// </summary>
        public void OnLoginClick()
        {
            //显示挡屏幕的登录中
            Get<RectTransform>("LoginingPanel").gameObject.SetActive(true);

            LoginEventArgs largs = new LoginEventArgs()
            {
                UserName = Get<InputField>("UserNameInputField").text,
                Password = Get<InputField>("PasswordInputField").text
            };

            //分发登录消息
            _messageQueue.DispatchMessage(LoginMsg.Login, this, largs);
        }

        /// <summary>
        /// 登录结果的回调
        /// </summary>
        /// <param name="sender">发送者。</param>
        /// <param name="args">参数 <see cref="EventArgs"/> </param>
        public void LoginResult(object sender, EventArgs args)
        {
            //将挡屏的登录中给隐藏
            Get<RectTransform>("LoginingPanel").gameObject.SetActive(false);

            LoginReplyEventArgs largs = (LoginReplyEventArgs)args;

            //提示登录消息
            UIManager.Instance.Open("DialogUI", 2, largs.Message);
        }

        /// <summary>
        /// 当按下退出按钮时。
        /// </summary>
        public void OnExitClick()
        {
            Application.Quit();
        }
    }
}